//
//  XRcordDetailsViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XRcordDetailsViewController.h"
#import "XRcordEditViewController.h"

@interface XRcordDetailsViewController ()


@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// day
@property(nonatomic,strong)UILabel* dayLb;

/// 星期
@property(nonatomic,strong)UILabel* weekLb;

/// 月
@property(nonatomic,strong)UILabel* monthLb;

/// content
@property(nonatomic,strong)UILabel* contentLb;


@property(nonatomic,strong)MyLinearLayout *bottomLayout;

/// 删除
@property(nonatomic,strong)UIButton* delBtn;

/// 编辑
@property(nonatomic,strong)UIButton* editBtn;


@end

@implementation XRcordDetailsViewController


- (void)loadView{
    [super loadView];
    // 线性布局
    self.view.backgroundColor = COLOR_BACKGEOUND;
    
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];

    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [rootLayout addSubview:scrollView];
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    //底部
    MyLinearLayout *bottomLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    bottomLayout.backgroundColor = UIColor.whiteColor;
    bottomLayout.myHeight = IPHONE_TAB_BAR_HEIGHT;
    bottomLayout.myHorzMargin = 0;
    bottomLayout.myMargin = 0;
    bottomLayout.padding = UIEdgeInsetsMake(0, 0, IPHONE_BOTTOM_SAFEINSET, 0);
    bottomLayout.gravity = MyGravity_Horz_Fill;
    [rootLayout addSubview:bottomLayout];
    _bottomLayout = bottomLayout;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"记事";
    
    [self setupViews];
    
    [self updateViews];
}

- (void)setupViews{
    
    MyRelativeLayout* dateLayout = [MyRelativeLayout new];
    dateLayout.myHorzMargin = 0;
    dateLayout.myTop = 20;
    dateLayout.padding = UIEdgeInsetsMake(0, 20, 0, 20);
    dateLayout.myHeight = MyLayoutSize.wrap;
    [self.contentLayout addSubview:dateLayout];
    
    UILabel* dayLb = [UILabel lableWithFont:FONTBOLD_FIT(20) color:COLOR_BLACK_DARK text:NULL];
    dayLb.textAlignment = NSTextAlignmentCenter;
    dayLb.myCenterY = 0;
    dayLb.myLeft = 0;
    [dateLayout addSubview:dayLb];
    _dayLb = dayLb;


    UILabel* weekLb = [UILabel lableWithFont:FONTMID_FIT(14) color:COLOR_BLACK_DARK text:NULL];
    weekLb.leftPos.equalTo(dayLb.rightPos).offset(16);
    weekLb.bottomPos.equalTo(dayLb.centerYPos).offset(4.0);
    [dateLayout addSubview:weekLb];
    _weekLb = weekLb;


    UILabel* monthLb = [UILabel lableWithFont:FONTMID_FIT(14) color:COLOR_BLACK_DARK text:NULL];
    monthLb.leftPos.equalTo(dayLb.rightPos).offset(16);
    monthLb.topPos.equalTo(dayLb.centerYPos).offset(4.0);
    [dateLayout addSubview:monthLb];
    _monthLb = monthLb;
    
    
    UILabel *contentLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_BASE text:NULL];
    contentLb.myHorzMargin = 20;
    contentLb.myTop = 32;
    contentLb.myHeight = MyLayoutSize.wrap;
    [self.contentLayout addSubview:contentLb];
    _contentLb = contentLb;
    

    UIButton* delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [delBtn setTitle:@"删除" forState:UIControlStateNormal];
    [delBtn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
    [delBtn addTarget:self action:@selector(delRecord:) forControlEvents:UIControlEventTouchUpInside];
    delBtn.titleLabel.font = FONT(14);
    delBtn.myCenterY = 0;
    delBtn.mySize = CGSizeMake(72, 40);
    [self.bottomLayout addSubview:delBtn];
    
    UIButton* editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [editBtn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(editRecord:) forControlEvents:UIControlEventTouchUpInside];
    editBtn.titleLabel.font = FONT(14);
    editBtn.myCenterY = 0;
    editBtn.mySize = CGSizeMake(72, 40);
    [self.bottomLayout addSubview:editBtn];

}


- (void)updateViews{
    
    self.dayLb.text = [NSString stringWithFormat:@"%ld",self.record.date.day];
    [self.dayLb sizeToFit];
    

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //自定义星期显示
    dateFormatter.weekdaySymbols = @[@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六"];
    //自定义日期显示
    dateFormatter.dateFormat = @"EEEE";
    
    self.weekLb.text = [dateFormatter stringFromDate:self.record.date];
    [self.weekLb sizeToFit];
    
    dateFormatter.dateFormat = @"yyyy/MM";
    
    self.monthLb.text = [dateFormatter stringFromDate:self.record.date];
    [self.monthLb sizeToFit];


    self.contentLb.text = self.record.content;
    [self.contentLb sizeToFit];

}

- (void)deleteRecord{
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        [[RLMRealm defaultRealm] deleteObject:self.record];
    }];
}

#pragma mark  --- action
/// 编辑
- (void)editRecord:(UIButton*)sender{
    
    XRcordEditViewController* editController = [XRcordEditViewController new];
    editController.operation = EditOperation_Edit;
    editController.record = self.record;
    editController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        [self updateViews];
        if(self.editBlock){
            self.editBlock(obj, EditOperation_Edit);
        }
    };
    [UIViewController showViewController:editController animated:YES];
}

/// 删除
- (void)delRecord:(UIButton*)sender{
    
    [Alert confirm:@"删除" message:@"你确定要删除这条记录吗?" completion:^(BOOL Bool) {
        if(Bool){
            [self deleteRecord];
            
            if(self.editBlock){
                self.editBlock(NULL, EditOperation_Delete);
            }
            
            [self closeViewControllerAnimated:YES];
        }
    }];
}

@end
